﻿function GetUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function CloudAjax(sync, cache, type, processData, url, contenttype, data, success, error, before) { //封装ajax的一些常用参数  //data数据可以为空
    var Token = GetLocalData("Token");
    if (Token && Token != "") {
        data.Token = Token;
    }
    $.ajax({
        sync: sync,
        cache: cache,
        type: type,
        url: url,
        contentType: contenttype,
        processData: processData,
        data: data,
        headers: {
            //"Authorization":$.cookie('token')
        },
        beforSend: function (request) {
            if (before) {
                before()
            }
        },
        error: function (data) {
            if (error) {
                error(data);
            }
        },
        complete: function (data, status) {

        },
        success: function (data) {
            if (success) {
                success(data)
            }
        }
    });
}

function CloudPost(data, url, success) {
    CloudAjax("true", "false", "POST", true, url, "application/json;charset=UTF-8", data, success)
}

function CloudPostSync(data, url, success) {
    CloudAjax("false", "false", "POST", true, url, "application/json;charset=UTF-8", data, success)
}

function CloudPostFile(data, url, success) {
    CloudAjax("true", "false", "POST", false, url, false, data, success)
}

function CloudGet(url, success) {
    CloudAjax("true", "false", "GET", true, url, "application/x-www-form-urlencoded; charset=UTF-8", "", success)
}

function GetNewGuid() {
    var date = new Date();
    return DateToString(date) + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

function DateToString(now) {
    var year = now.getFullYear();
    var month = (now.getMonth() + 1).toString();
    var day = (now.getDate()).toString();
    var hour = (now.getHours()).toString();
    var minute = (now.getMinutes()).toString();
    var second = (now.getSeconds()).toString();
    if (month.length == 1) {
        month = "0" + month;
    }
    if (day.length == 1) {
        day = "0" + day;
    }
    if (hour.length == 1) {
        hour = "0" + hour;
    }
    if (minute.length == 1) {
        minute = "0" + minute;
    }
    if (second.length == 1) {
        second = "0" + second;
    }
    var dateTime = year + month + day + hour + minute + second;
    return dateTime;
}